function UI_isElement ( ele )
	if ( isElement ( ele ) and UI_Elements [ getElementType ( ele ) ] ) then
		return true
	end
	return false
end

function UI_GetPosition ( ele )
	if ( UI_isElement ( ele ) ) then
		return getElementData ( ele, "UI_X" ) or 0, getElementData ( ele, "UI_Y" ) or 0
	end
	return false
end

function UI_SetPosition ( ele, x, y )
	if ( UI_isElement ( ele ) ) then
		local retX = setElementData ( ele, "UI_X", x )
		local retY = setElementData ( ele, "UI_Y", y )
		if ( retX or retY ) then
			return true
		end
	end
	return false
end

function UI_GetSize ( ele )
	if ( UI_isElement ( ele ) ) then
		return getElementData ( ele, "UI_Width" ) or 0, getElementData ( ele, "UI_Height" ) or 0
	end
	return false
end

function UI_SetSize ( ele, width, height )
	if ( UI_isElement ( ele ) ) then
		local retWidth = setElementData ( ele, "UI_Width", width )
		local retHeight = setElementData ( ele, "UI_Height", height )
		if ( retWidth or retHeight ) then
			destroyElement ( getElementData ( ele, "UI_RenderTarget" ) )
			setElementData ( ele, "UI_RenderTarget", dxCreateRenderTarget ( width > 1 and width or 1, height > 1 and height or 1, true ) )
			if ( getElementData ( ele, "UI_RenderTarget2" ) ) then
				destroyElement ( getElementData ( ele, "UI_RenderTarget2" ) )
				local Theme = getElementData ( ele, "UI_Theme" ) or UI_ThemeDefault
				local ThemeWidthLeft = 0
				local ThemeWidthRight = 0
				local ThemeHeightTop = 0
				local ThemeHeightBottom = 0
				if ( getElementType ( ele ) == "UI_Window" ) then
					ThemeWidthLeft = UI_ThemeGetWidth ( "Frame2Left", Theme )
					ThemeWidthRight = UI_ThemeGetWidth ( "Frame2Right", Theme )
					ThemeHeightTop = UI_ThemeGetHeight ( "TitleBarBackground", Theme )
					ThemeHeightBottom = UI_ThemeGetHeight ( "Frame2Bottom", Theme )
				elseif ( getElementType ( ele ) == "UI_Button" ) then
					ThemeWidthLeft = UI_ThemeGetWidth ( "ButtonLeft", Theme )
					ThemeWidthRight = UI_ThemeGetWidth ( "ButtonRight", Theme )
					ThemeHeightTop = UI_ThemeGetHeight ( "ButtonTop", Theme )
					ThemeHeightBottom = UI_ThemeGetHeight ( "ButtonBottom", Theme )
				end
				width = width - ThemeWidthLeft - ThemeWidthRight
				height = height - ThemeHeightTop - ThemeHeightBottom
				setElementData ( ele, "UI_RenderTarget2", dxCreateRenderTarget ( width > 1 and width or 1, height > 1 and height or 1 ) )
			end
			return true
		end
	end
	return false
end

function UI_GetFont ( ele )
	if ( UI_isElement ( ele ) ) then
		return getElementData ( ele, "UI_Font" ) or "default-bold"
	end
	return false
end

function UI_SetFont ( ele, font )
	if ( UI_isElement ( ele ) ) then
		local Theme = getElementData ( ele, "UI_Theme" ) or UI_ThemeDefault
		local Height = tonumber ( UI_ThemeGetHeight ( "TitleBarBackground", Theme ) )
		local Size = 0
		if ( isElement ( font ) and getElementType ( font ) == "UI_Font" ) then
			if ( getElementType ( ele ) == "UI_Window" ) then
				for i = 1, Height do
					UI_FontSetSize ( font, i )
					if ( dxGetFontHeight ( 1, getElementData ( font, "UI_Font" ) ) >= Height ) then
						Size = i
						break
					end
				end
			end
			local ret1 = setElementData ( ele, "UI_Font", font )
			local ret2 = setElementData ( ele, "UI_FontSize", 1 )
			if ( ret1 or ret2 ) then
				if ( getElementType ( ele ) == "UI_Window" ) then UI_TitleFont[ele] = dxCreateFont ( getElementData ( font, "UI_FontFilepath" ), Size, getElementData ( font, "UI_FontBold" ) ) end
				return true
			end
		else
			if ( getElementType ( ele ) == "UI_Window" ) then
				for i = 1, Height do
					if ( dxGetFontHeight ( i, font ) >= Height ) then
						Size = i
						break
					end
				end
			end
			local ret1 = setElementData ( ele, "UI_Font", font )
			local ret2 = setElementData ( ele, "UI_FontSize", Size )
			if ( ret1 or ret2 ) then
				return true
			end
		end
	end
	return false
end

function UI_GetFontSize ( ele )
	if ( UI_isElement ( ele ) ) then
		if ( getElementType ( getElementData ( ele, "UI_Font" ) ) == "UI_Font" ) then
			return UI_FontGetSize ( ele ) or 9
		else
			return getElementData ( ele, "UI_FontSize" ) or 1
		end
	end
	return false
end

function UI_SetFontSize ( ele, size )
	if ( UI_isElement ( ele ) and getElementType ( ele ) ~= "UI_Window" ) then
		if ( setElementData ( ele, "UI_FontSize", size ) ) then
			return true
		end
	end
	return false
end

function UI_GetColor ( ele )
	if ( UI_isElement ( ele ) ) then
		local RGB = getElementData ( ele, "UI_RGB" )
		return RGB["Red"] or 255, RGB["Green"] or 255, RGB["Blue"] or 255
	end
	return false
end

function UI_SetColor ( ele, red, green, blue )
	if ( UI_isElement ( ele ) ) then
		if ( setElementData ( ele, "UI_RGB", { ["Red"] = red or 255, ["Green"] = green or 255, ["Blue"] = blue or 255 } ) ) then
			return true
		end
	end
	return false
end

function UI_GetText ( ele )
	if ( UI_isElement ( ele ) ) then
		return getElementData ( ele, "UI_Text" ) or ""
	end
	return false
end

function UI_SetText ( ele, text )
	if ( UI_isElement ( ele ) ) then
		if ( setElementData ( ele, "UI_Text", text ) ) then
			return true
		end
	end
	return false
end

function UI_GetVisible ( ele )
	if ( UI_isElement ( ele ) ) then
		return getElementData ( ele, "UI_Visible" )
	end
	return false
end

function UI_SetVisible ( ele, toggle )
	if ( UI_isElement ( ele ) ) then
		if ( setElementData ( ele, "UI_Visible", toggle ) ) then
			return true
		end
	end
	return false
end

function UI_GetAlpha ( ele )
	if ( UI_isElement ( ele ) ) then
		return getElementData ( ele, "UI_Alpha" )
	end
	return false
end

function UI_SetAlpha ( ele, value )
	if ( UI_isElement ( ele ) ) then
		if ( setElementData ( ele, "UI_Alpha", value ) ) then
			return true
		end
	end
	return false
end

function UI_GetColorCoded ( ele )
	if ( UI_isElement ( ele ) ) then
		return getElementData ( ele, "UI_ColorCoded" )
	end
	return false
end

function UI_SetColorCoded ( ele, toggle )
	if ( UI_isElement ( ele ) ) then
		if ( setElementData ( ele, "UI_ColorCoded", toggle ) ) then
			return true
		end
	end
	return false
end

function UI_isMouseInElements ( pointx, pointy )
	for i = 1, #UI_Render do
		local ele = UI_Render[( #UI_Render + 1 ) - i]
		if ( UI_isElement ( ele ) and UI_GetVisible ( ele ) ) then
			local X = getElementData ( ele, "UI_X" ) or 0
			local Y = getElementData ( ele, "UI_Y" ) or 0
			local Width = getElementData ( ele, "UI_Width" ) or 0
			local Height = getElementData ( ele, "UI_Height" ) or 0
			local Theme = getElementData ( ele, "UI_Theme" ) or UI_ThemeDefault
			local ThemeHeight = UI_ThemeGetHeight ( "TitleBarBackground", Theme ) or 0
			if ( pointx >= X and pointx <= X + Width ) and ( pointy >= Y and pointy <= Y + Height ) then
				if ( pointx >= X and pointx <= X + Width ) and ( pointy >= Y and pointy <= Y + ThemeHeight ) then
					UI_OldElement = ele
					return ele
				elseif ( UI_RenderParent[ele] ) then
					X = X + UI_ThemeGetWidth ( "Frame2Left", Theme )
					Y = Y + UI_ThemeGetHeight ( "TitleBarBackground", Theme )
					for i = 1, #UI_RenderParent[ele] do
						local eleparent = UI_RenderParent[ele][( #UI_RenderParent[ele] + 1 ) - i]
						if ( UI_isElement ( eleparent ) and UI_GetVisible ( eleparent ) ) then
							local ParentX = getElementData ( eleparent, "UI_X" ) or 0
							local ParentY = getElementData ( eleparent, "UI_Y" ) or 0
							local ParentWidth = getElementData ( eleparent, "UI_Width" ) or 0
							local ParentHeight = getElementData ( eleparent, "UI_Height" ) or 0

							ParentX = ele and ParentX + X or ParentX
							ParentY = ele and ParentY + Y or ParentY

							if ( pointx >= ParentX and pointx <= ParentX + ParentWidth ) and ( pointy >= ParentY and pointy <= ParentY + ParentHeight ) then
								UI_OldElement = eleparent
								return eleparent
							end
						end
					end
				end
				UI_OldElement = ele
				return ele
			end
		end
	end
end

function UI_CheckMouseOutElements ( pointx, pointy )
	for i = 1, #UI_Render do
		local ele = UI_Render[( #UI_Render + 1 ) - i]
		if ( UI_isElement ( ele ) and UI_GetVisible ( ele ) ) then
			if ( UI_isMouseOutElement ( ele, pointx, pointy ) ) then
				local MouseState = getElementData ( ele, "UI_EventMouseState" ) or false
				if ( MouseState ) then
					setElementData ( ele, "UI_EventMouseState", false )
					triggerEvent ( "onUI_MouseLeave", ele, pointx, pointy )
				end
				if ( UI_RenderParent[ele] ) then
					for i = 1, #UI_RenderParent[ele] do
						local eleparent = UI_RenderParent[ele][( #UI_RenderParent[ele] + 1 ) - i]
						if ( UI_isElement ( eleparent ) and UI_GetVisible ( eleparent ) ) then
							local MouseState = getElementData ( eleparent, "UI_EventMouseState" ) or false
							if ( MouseState ) then
								setElementData ( eleparent, "UI_EventMouseState", false )
								triggerEvent ( "onUI_MouseLeave", eleparent, pointx, pointy )
							end
						end
					end
				end
			else
				if ( UI_RenderParent[ele] ) then
					for i = 1, #UI_RenderParent[ele] do
						local eleparent = UI_RenderParent[ele][( #UI_RenderParent[ele] + 1 ) - i]
						if ( UI_isElement ( eleparent ) and UI_GetVisible ( ele ) ) then
							if ( UI_isMouseOutElement ( eleparent, pointx, pointy ) ) then
								local MouseState = getElementData ( eleparent, "UI_EventMouseState" ) or false
								if ( MouseState ) then
									setElementData ( eleparent, "UI_EventMouseState", false )
									triggerEvent ( "onUI_MouseLeave", eleparent, pointx, pointy )
								end
							end
						end
					end
				end
			end
		end
	end
end

function UI_isMouseOutElement ( ele, pointx, pointy )
	if ( UI_isElement ( ele ) ) then
		local X = getElementData ( ele, "UI_X" ) or 0
		local Y = getElementData ( ele, "UI_Y" ) or 0
		local Width = getElementData ( ele, "UI_Width" ) or 0
		local Height = getElementData ( ele, "UI_Height" ) or 0
		local Theme = getElementData ( ele, "UI_Theme" ) or UI_ThemeDefault
		local ThemeTitleBarWidth = UI_ThemeGetWidth ( "TitleBarBackground", Theme ) or 0
		local ThemeTitleBarHeigth = UI_ThemeGetHeight ( "TitleBarBackground", Theme ) or 0
		local ThemeFrame2Left = UI_ThemeGetWidth ( "Frame2Left", Theme ) or 0
		
		local CheckParent = getElementParent ( ele ) ~= UI_Root
		local Parent = CheckParent and getElementParent ( ele ) or false
		local ParentTheme = Parent and getElementData ( Parent, "UI_Theme" ) or UI_ThemeDefault

		if ( getElementType ( Parent ) == "UI_Window" ) then
			X = X + UI_ThemeGetWidth ( "Frame2Left", ParentTheme )
			Y = Y + UI_ThemeGetHeight ( "TitleBarBackground", ParentTheme )
		end

		X = Parent and X + ( getElementData ( Parent, "UI_X" ) or 0 ) or X
		Y = Parent and Y + ( getElementData ( Parent, "UI_Y" ) or 0 ) or Y

		if ( pointx >= X and pointx <= X + Width ) and ( pointy >= Y and pointy <= Y + Height ) then
			return false
		end
	end
	return true
end